package homepage

import (
	"strconv"

	"github.com/gin-gonic/gin"
	"go-papers/models"
	"go-papers/pkg/lognew"
	"go-papers/tools"
	"go-papers/tools/app"
)

func GetOrganList(c *gin.Context) {
	var data models.Organ
	var err error
	var pageSize = 10
	var pageIndex = 1

	ss := models.SearchReq{}
	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("organBrandId"); index != "" {
		ss.OrganBrandId = tools.StrToInt(err, index)
	}

	ss.OrganBrand = c.Request.FormValue("organBrand")
	ss.OrganName = c.Request.FormValue("organName")
	ss.OrganAddr = c.Request.FormValue("organAddr")
	ss.OrganFeature = c.Request.FormValue("organFeature")
	ss.OrganDesc = c.Request.FormValue("organDesc")
	ss.KeyWord = c.Request.FormValue("keyword") // keyword
	ss.CourseType = c.Request.FormValue("courseType")

	ss.MemberId = tools.GetMemberId(c)
	ss.OpenId = c.Request.FormValue("openId")
	ss.LocationKm = c.Request.FormValue("locationKm")
	ss.LocationKmType = c.Request.FormValue("locationKmType")

	result, count, err := data.GetPageSearch(pageSize, pageIndex, ss)
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "成功")
}

// GetOrgan
func GetOrgan(c *gin.Context) {
	var data models.Organ
	data.OrganId, _ = tools.StringToInt(c.Param("id"))
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

// GetOrganCourse
func GetOrganCourse(c *gin.Context) {
	var data models.Course
	var err error
	var pageSize = 20
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("organId"); index != "" {
		data.OrganId = tools.StrToInt(err, index)
	}
	data.CourseStatus = "5"
	data.CourseName = c.Request.FormValue("courseName")
	data.CourseType = c.Request.FormValue("courseType")
	data.CourseCategory = c.Request.FormValue("courseCategory")
	data.OrganName = c.Request.FormValue("organName")
	data.BrandName = c.Request.FormValue("brandName")
	data.CourseTeach = c.Request.FormValue("courseTeach")
	if index := c.Request.FormValue("coursePrice"); index != "" {
		data.CoursePrice = tools.StringToFloat64(err, index)
	}
	if index := c.Request.FormValue("discountPrice"); index != "" {
		data.DiscountPrice = tools.StringToFloat64(err, index)
	}
	data.CourseTime = c.Request.FormValue("courseTime")
	data.CourseAge = c.Request.FormValue("courseAge")

	result, count, err := data.GetSearchPage(pageSize, pageIndex) // gateway
	if err != nil {
		tools.HasError(err, "网络异常，请稍后重试"+err.Error(), -1)
	}
	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func OrganMsg(c *gin.Context) {

	organId := c.Request.FormValue("organId")
	openId := c.Request.FormValue("openid")

	//查询授权数据
	var tt models.ThirdUser
	tt.ThridOpenId = openId
	tInfo, err := tt.Get()
	if err != nil {
		app.ApiResp(c, -1, "微信授权用户信息为查询到", nil)
		return
	}
	var ll models.OrganMsg
	ll.OrganId, _ = strconv.Atoi(organId)
	ll.OpenId = openId

	// 先查询
	ii, _ := ll.Get()

	if ii.Id > 0 {

		ll.OpenUserName = tInfo.ThridNickName
		ll.OpenUserId = tInfo.ThridUserId
		ll.Status = 1
		ll.MsgRole = "1"

		_, err = ll.Update(ii.Id)
		if err != nil {
			lognew.Info(c, "msg_update", lognew.Fields{"ll": ll, "err": err.Error()})
		}
	} else {

		ll.OpenUserName = tInfo.ThridNickName
		ll.OpenUserId = tInfo.ThridUserId
		ll.Status = 1
		ll.MsgRole = "1"

		_, err = ll.Create()
		if err != nil {
			lognew.Info(c, "msg_insert", lognew.Fields{"ll": ll, "err": err.Error()})
		}
	}

	res := gin.H{
		"organId": organId,
		"openId":  openId,
	}
	app.ApiResp(c, 0, "绑定成功", res)

}
